<?php

namespace app\store\model\user;

use app\common\model\BaseModel;
use app\store\model\User as UserModel;
use app\api\model\Order;

class UserLabel extends BaseModel
{
    /**
     * 获取列表记录
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $list=$this->with(['image'])->where('is_delete', '=', 0)
            ->order(['weight' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);

        return $list;
    }

    public function getApiOne($label_id){
        $data=$this->with(['image'])->where(['label_id'=>$label_id,'status'=>1,'is_delete'=>0])->find();
        return $data;
    }

    public function getApiHighLow(){
        $data['highest_name']=self::where(['wxapp_id'=>self::$wxapp_id,'pay_amount'=>['<>',0],'status'=>1,'is_delete'=>0])->order('weight asc')->value('name');
        $data['lowest_name']=self::where(['wxapp_id'=>self::$wxapp_id,'pay_amount'=>['<>',0],'status'=>1,'is_delete'=>0])->order('weight desc')->value('name');
        return $data;
    }

    //获取当前等级的下一级数据
    public function getApiNext($label_id){
        $weight=self::where(['label_id'=>$label_id,'status'=>1,'wxapp_id'=>self::$wxapp_id])->value('weight');
        $data=$this->where(['status'=>1,'weight'=>['>',$weight]])->find();
        return $data;
    }

    public function getApiAll($label_id){
        $data=$this->with(['image'])->where(['wxapp_id'=>self::$wxapp_id,'status'=>1,'is_delete'=>0])->order('weight asc')->select();
        return $data;
    }

    public function image()
    {
        return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_id');
    }

    public static function getPayAmountZeroId(){
        return self::where(['pay_amount'=>0])->value('label_id');
    }

    //用于判断当前用户的消费金额并返回等级ID
    public function getUserLabel($user_id){
        $orderModel=new Order();
        $total_price=$orderModel->getAllOrderPrice($user_id);
        $next_label_id=self::where(['pay_amount'=>['<=',$total_price],'wxapp_id'=>self::$wxapp_id])->order('weight desc')->value('label_id');
        return $next_label_id;
    }


    public static function getUsableList($order = ['weight' => 'asc'])
    {

        return self::where('status', '=', '1')
            ->where('is_delete', '=', '0')
            ->where('wxapp_id', '=', self::$wxapp_id)
            ->order($order)
            ->select();
    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return false|int
     */
    public function edit($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        return $this->allowField(true)->save($data) !== false;
    }


    public static function detail($label_id, $with = [])
    {
        return static::get($label_id, $with);
    }
    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        // 判断该等级下是否存在会员
        if (UserModel::checkExistByLabelId($this['label_id'])) {
            $this->error = '该标签等级下存在用户，不允许删除';
            return false;
        }
        return $this->save(['is_delete' => 1]);
    }
}
